﻿using System.Drawing.Drawing2D;

namespace VideoTools.Controls
{
    public interface MaskPictureBoxCallback
    {
        /// <summary>
        /// 为了回调获取是否画了遮罩
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void picture_MouseMove(object sender, MouseEventArgs e);

        /// <summary>
        /// 清理遮罩后回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void mask_Clear();
    }
    public partial class MaskPictureBox : ScrollableControl
    {
        Color backColor = Color.FromArgb(0, 0, 0, 0);
        Bitmap originImg;
        Image finishImg;
        Graphics g;
        Point StartPoint, EndPoint, FontPoint;
        Pen p = new Pen(Color.White, 40);
        bool IsDraw;
        Font font;
        Rectangle FontRect;
        bool drawed = false;
#pragma warning disable WFO1000
        public MaskPictureBoxCallback parent { get; set; }
        public Image Image
        {
            get => this.picbox.Image;
            set
            {
                if (value == null)
                {
                    return;
                }
                this.picbox.Image = value;
                this.picbox.BackgroundImage = value;
                this.picbox.Width = value.Width;
                this.picbox.Height = value.Height;

                this.pnlHost.Height = this.picbox.Height;
                this.pnlHost.Width = this.picbox.Width;

                this.Width = this.pnlHost.Width;
                this.Height = this.pnlHost.Height;
                p.StartCap = LineCap.Round;
                p.EndCap = LineCap.Round;

                originImg = new Bitmap(picbox.Width, picbox.Height);
                g = Graphics.FromImage(originImg);

                g.Clear(backColor);

                picbox.Image = originImg;
                finishImg = (Image)originImg.Clone();
            }
        }
        

        public Image getMaskImage(bool mixBlackImage)
        {
            if (!drawed)
            {
                return null;
            }
            Image saveImage = finishImg;
            if (mixBlackImage)
            {
                Bitmap black = new Bitmap(finishImg.Width, finishImg.Height);

                Graphics blackGraphics = Graphics.FromImage(black);
                Brush blackBrush = new SolidBrush(Color.Black);
                blackGraphics.FillRectangle(blackBrush, new Rectangle(0, 0, finishImg.Width, finishImg.Height));

                blackGraphics.DrawImage(saveImage, 0, 0);
                saveImage = black;
            }
            return saveImage;

        }

#pragma warning disable WFO1000
        public Color DrawColor
        {
            get { return p.Color; }
            set { p.Color = value; }
        }
#pragma warning disable WFO1000
        public float PenWidth
        {
            get => p.Width;
            set { p.Width = value; }
        }
        public int RHeight
        {
            set
            {
                this.picbox.Height = value;
                this.pnlHost.Height = value;
                this.Height = this.pnlHost.Height;

            }
            get
            {
                return this.Height;
            }
        }
        public int RWidth
        {
            set
            {
                this.picbox.Width = value;
                this.pnlHost.Width = value;
                this.Width = this.pnlHost.Width;

            }
            get
            {
                return this.Width;
            }
        }
        public MaskPictureBox()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.UserPaint, true);
            this.UpdateStyles();

        }


        private void picture_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                IsDraw = true;
                StartPoint = e.Location;
                finishImg = (Image)originImg.Clone();
            }
        }

        public void Save(string path, bool mixBlackImage = false)
        {
            if (!drawed)
            {
                return;
            }
            Image saveImage = finishImg;
            if (mixBlackImage)
            {
                Bitmap black = new Bitmap(finishImg.Width, finishImg.Height);

                Graphics blackGraphics = Graphics.FromImage(black);
                Brush blackBrush = new SolidBrush(Color.Black);
                blackGraphics.FillRectangle(blackBrush, new Rectangle(0, 0, finishImg.Width, finishImg.Height));

                blackGraphics.DrawImage(saveImage, 0, 0);
                saveImage = black;
            }

            saveImage.Save(path);
        }


        private void picture_MouseMove(object sender, MouseEventArgs e)
        {

            if (IsDraw)
            {
                EndPoint = e.Location;
                /*                if (dType != DrawType.Pen && dType != DrawType.Eraser)
                                {
                                    finishImg = (Image)originImg.Clone();
                                }*/
                g = Graphics.FromImage(finishImg);
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.DrawLine(p, StartPoint, EndPoint);
                StartPoint = EndPoint;
                reDraw();
                parent.picture_MouseMove(sender, e);
            }
        }

        private void picture_MouseUp(object sender, MouseEventArgs e)
        {
            IsDraw = false;
            originImg = (Bitmap)finishImg;


            picbox.Image = originImg;
        }
        private void reDraw()
        {
            Graphics graphics = picbox.CreateGraphics();
            graphics.DrawImage(finishImg, new Point(0, 0));
            graphics.Dispose();
            drawed = true;
        }

        public void Clear()
        {
            g.Clear(backColor);

            reDraw();
            drawed = false;
            parent.mask_Clear();
        }
    }
}
